package com.yuancheng.common.util;

import java.util.Random;

/**
 * 随机数
 * Created by Administrator on 2016/12/24.
 */
public class RandomUtil {
    public static final int TYPE_CHAR = 1;
    public static final int TYPE_NUMBER= 2;
    public static final int TYPE_CHARORNUMBER = 3;

    public static final String STRING_CHAR = "ABCDEFGHJKLMNPRSTUVWXYZ";
    public static final String STRING_NUMBER = "0123456789";
    public static final String STRING_CHARORNUMBER = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";

    public static String getRandom(int length, int type){
        String code = "";
        String s = getStringByType(type);
        for(int i=0; i<length; i++){
            code += randomCharOrNumber(s);
        }
        return code;
    }

    private static String getStringByType(int type){
        String s = "";
        if(type == TYPE_CHAR){
            s = STRING_CHAR;
        }else if(type == TYPE_NUMBER){
            s = STRING_NUMBER;
        }else if(type == TYPE_CHARORNUMBER){
            s = STRING_CHARORNUMBER;
        }
        return s;
    }

    private static char randomCharOrNumber(String s){
        Random r = new Random();
        return s.charAt(r.nextInt(s.length()));
    }
}

